global class proformaInvoiceBatchDelete implements Database.Batchable<sObject>
{
    String query;
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        if(!Test.isRunningTest())
            query = 'SELECT Id,isPerformaInvoice__c FROM Invoice__c where isPerformaInvoice__c =' + true + ' LIMIT 10000';
        else
            query = 'SELECT Id,isPerformaInvoice__c FROM Invoice__c where isPerformaInvoice__c =' + true + ' LIMIT 200';    
        
        return Database.getQueryLocator(query);
    }

   global void execute(Database.BatchableContext BC, List<Invoice__c> performaInvoiceList)
   {
        system.debug('Performa Invoice List : '+ performaInvoiceList);
        
        if(!Test.isRunningTest())
        {
            //First Delete the List...
            Database.delete(performaInvoiceList, false);
            
            //Then Delete from the Recycle bin also....
            Database.emptyRecycleBin(performaInvoiceList);
        }
    }

    global void finish(Database.BatchableContext BC)
    {
        List<String> emailAddress = new List<String>();
        String emailSubject = ''; 
        string emailBody = '';
        
        AsyncApexJob asyncJob = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email
                                 From AsyncApexJob Where Id = :BC.getJobId()];
       
        emailAddress.add(Internal_Email_Administrator__c.getInstance('Proforma Invoice Notification').Administrator_Email__c);
               
        emailSubject = Internal_Email_Administrator__c.getInstance('Proforma Invoice Notification').Email_Subject__c;
        emailSubject = emailSubject.replace('<ProcessName>', 'Proforma Invoice Deletion Notification');
                
        emailBody = Internal_Email_Administrator__c.getInstance('Proforma Invoice Notification').Email_Body__c;
        
        emailBody = emailBody.replace('<JobStatus>', asyncJob.Status);
        emailBody = emailBody.replace('<TotalJobItems>', String.valueOf(asyncJob.TotalJobItems));
        emailBody = emailBody.replace('<TotalJobItemProcessed>', String.valueOf(asyncJob.JobItemsProcessed));
        emailBody = emailBody.replace('<Errors>', String.valueOf(asyncJob.NumberOfErrors));
        
        emailUtilityClass.sendHTMLEmail(emailAddress, emailSubject, emailBody);
   }
       
   
}